package moriarty.gwt.server.persistence;


import moriarty.gwt.shared.DataStoreObject;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public final class DataStoreManager {
	
	public static Key<DataStoreObject> put(DataStoreObject data){
		Objectify ofy = ObjectifyService.begin();
		Key<DataStoreObject> key = ofy.put(data);
		return key;
	}
	
	public static <T> Query<T> queryClass(Class<T> type){
		Objectify ofy = ObjectifyService.begin();
		Query<T> q = ofy.query(type);
		return q;
	}
	
	public static <T> T get(Class<T> type, Long id){
		Objectify ofy = ObjectifyService.begin();
		T fetched = ofy.get(type, id);
		return fetched;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> Key<T> update(Class<T> type, DataStoreObject newObject){
		Objectify ofy = ObjectifyService.begin();
		T oldObject = ofy.get(type, newObject.id);
		oldObject = (T) newObject;
		Key<T> key = ofy.put(oldObject);
		return key;
	}
}
